// tbkPackage/pages/proList.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        channel: "jd",
        type: "hot",
        value: "1",
        page: 1,
        proList: [],
        leader: 0,
        commissionname: "返利",
        floorstatus: false,
        noshare: true,
        noData: false,
        search_con: "",
        cate_modlue: [],
        cate: 1,
        //1 京东 2 拼多多
        cid: "",
        //分类
        cateShow: 0,
        //分类弹框
        sec: "desc",
        //倒叙desc 升序asc
        sort: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        wx.hideShareMenu();
        that.getBase();
        that.getMyLeaderInfo();
        var cate_modlue = [];
        if (options.st == "1") {
            that.goodsCats();
            //拼多多分类
                        cate_modlue = [ {
                id: 1,
                title: "拼多多"
            } ];
        } else if (options.st == "2") {
            that.jingCats();
            cate_modlue = [ {
                id: 2,
                title: "京东"
            } ];
        } else if (options.st == "1,2") {
            that.goodsCats();
            that.jingCats();
            cate_modlue = [ {
                id: 1,
                title: "拼多多"
            }, {
                id: 2,
                title: "京东"
            } ];
        }
        that.setData({
            cate_modlue: cate_modlue,
            cate: cate_modlue[0].id,
            domain: app.globalData.domain.slice(0, -10)
        });
        if (options.channel == "jd") {
            that.setData({
                cate: 2
            });
        }
        that.getSearchGoods();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        if (this.data.noshare == false) {
            this.setData({
                noshare: true
            });
        }
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        this.getProList(that.data.channelename, 1, that.data.type, that.data.value);
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        if (that.data.cate == 1) {
            //拼多多
            that.getPinGoods();
        } else {
            //京东
            that.getJinGoods();
        }
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        //当团长分享的时候 不显示团长佣金
                if (that.data.iam_leader) {
            this.setData({
                noshare: false
            });
        }
        return {
            title: title,
            path: "/tbkPackage/pages/proListSearch/proListSearch?st=" + that.data.st
        };
    },
    //全局基础配置信息，这个模块暂时只用头部颜色，新版无头的也可能不用了
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                // wx.setNavigationBarColor({
                //   frontColor: "#ffffff",
                //   backgroundColor: res.extra.color_main
                // })
                that.setData({
                    color_main: res.extra.color_main
                });
            }
        });
    },
    //获取团长信息
    getMyLeaderInfo: function() {
        var that = this;
        var user = wx.getStorageSync("user");
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: user.openid
            },
            success: res => {
                //判断哪个团长优先
                //1.我是团长，则优先用自己
                //2.本次分享团长 优先级 大于 历史绑定团长
                if (res.extra.iam_leader) {
                    var leader = res.extra.leader_id;
                } else {
                    var leader = that.data.leader != "" ? that.data.leader : res.extra.leader_id;
                }
                let commissionname = res.extra.iam_leader == 1 ? "返佣" : "返利";
                that.setData({
                    iam_leader: res.extra.iam_leader,
                    leader: leader,
                    commissionname: commissionname
                });
                //获取佣金比例
                                common.request({
                    endpoint: "Taoke/common/brokerage-setting-view",
                    method: "post",
                    data: {},
                    success: res => {
                        //根据身份，判断显示的佣金或返利比例
                        if (that.data.iam_leader) {
                            var commission = res.extra.is_taoke_leader_brokerage == 1 ? res.extra.taoke_leader_brokerage_rate : 0;
                        } else {
                            var commission = res.extra.is_taoke_user_brokerage == 1 ? res.extra.taoke_user_brokerage_rate : 0;
                        }
                        that.setData({
                            commission: commission,
                            commissionUser: res.extra.is_taoke_user_brokerage
                        });
                    }
                });
            }
        });
    },
    cateShow: function() {
        let that = this;
        let cateShow = that.data.cateShow == 1 ? "0" : "1";
        that.setData({
            cateShow: cateShow
        });
    },
    choosePriceSort: function() {
        //价格升序降序
        let that = this;
        if (that.data.cate == 1) {
            //拼多多
            that.setData({
                sort: that.data.sort == 1 ? 3 : 1
            });
        } else {
            //京东
            that.setData({
                sort: 1,
                sec: that.data.sec == "desc" ? "asc" : "desc"
            });
        }
        that.getSearchGoods();
    },
    chooseBrokerageSort: function() {
        //价格升序降序
        let that = this;
        if (that.data.cate == 1) {
            //拼多多
            that.setData({
                sort: that.data.sort == 2 ? 4 : 2
            });
        } else {
            //京东
            that.setData({
                sort: 2,
                sec: that.data.sec == "desc" ? "asc" : "desc"
            });
        }
        that.getSearchGoods();
    },
    chooseCate: function(e) {
        //选择拼多多还是京东
        let that = this;
        that.setData({
            cate: e.currentTarget.dataset.cate,
            page: 1,
            cid: ""
        });
        that.getSearchGoods();
    },
    chooseCat: function(e) {
        //选择分类
        let that = this;
        let cid;
        if (that.data.cid != e.currentTarget.dataset.id) {
            cid = e.currentTarget.dataset.id;
        } else {
            cid = "";
        }
        that.setData({
            cid: cid,
            cateShow: 0
        });
        that.jingCats();
        that.getSearchGoods();
    },
    //获取拼多多一级分类
    goodsCats: function() {
        let that = this;
        common.request({
            endpoint: "Taoke/common/goods-cats-get",
            method: "post",
            data: {},
            success: function(res) {
                that.setData({
                    pddCatsList: res.goods_cats_get_response.goods_cats_list
                });
            }
        });
    },
    //获取京东一级分类
    jingCats: function() {
        let that = this;
        common.request({
            endpoint: "Taoke/common/jing-category",
            method: "post",
            data: {},
            success: function(res) {},
            complete(res) {
                if (res.data.code == 200) {
                    that.setData({
                        JinCatsList: res.data.data
                    });
                }
            }
        });
    },
    //搜索商品
    getSearchGoods: function() {
        let that = this;
        that.setData({
            proList: [],
            page: 1
        });
        if (that.data.cate == 1) {
            //拼多多
            that.getPinGoods();
        } else {
            //京东
            that.getJinGoods();
        }
    },
    getJinGoods: function() {
        let that = this;
        common.request({
            endpoint: "Taoke/common/jin-cate",
            method: "post",
            data: {
                sort: that.data.sort,
                //1价格 2.佣金
                cid: that.data.cid,
                keyword: that.data.search_con,
                page: that.data.page,
                sec: that.data.sec,
                //倒叙desc 升序asc
                uid: wx.getStorageSync("user").uid || "-"
            },
            success: function(res) {
                let proList = that.data.page == 1 ? res.extra.data : that.data.proList.concat(res.extra.data);
                that.setData({
                    proList: proList,
                    channelename: "jingdong",
                    page: that.data.page + 1
                });
            }
        });
    },
    getPinGoods: function() {
        let that = this;
        common.request({
            endpoint: "Taoke/common/pin-cate",
            method: "post",
            data: {
                sort: that.data.sort,
                // sort=1价格降序 2佣金降序 3价格升序 4佣金
                cid: that.data.cid,
                keyword: that.data.search_con,
                page: that.data.page,
                uid: wx.getStorageSync("user").uid || "-"
            },
            success: function(res) {
                if (res.extra.generate && res.extra.generate == 1) {
                    //跳转到拼多多
                    wx.navigateToMiniProgram({
                        appId: res.extra.rp_promotion_url_generate_response.url_list[0].we_app_info.app_id,
                        path: res.extra.rp_promotion_url_generate_response.url_list[0].we_app_info.page_path,
                        success(res) {
                            console.log("navigate success");
                        }
                    });
                }
                let proList = that.data.page == 1 ? res.extra.goods_search_response.goods_list : that.data.proList.concat(res.extra.goods_search_response.goods_list);
                that.setData({
                    proList: proList,
                    channelename: "pinduoduo",
                    page: that.data.page + 1
                });
            }
        });
    },
    //跳转内页
    goToDetail: function(e) {
        wx.navigateTo({
            url: "/tbkPackage/pages/detail/detail?channel=" + this.data.channelename + "&id=" + e.currentTarget.dataset.id + "&leader=" + this.data.leader
        });
    },
    //监听下拉，显示返回顶部
    onPageScroll: function(e) {
        if (e.scrollTop > 870) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    // 一键回到顶部
    goTop: function() {
        wx.pageScrollTo({
            scrollTop: 0
        });
    },
    //记录搜索名称
    saveWord: function(e) {
        var that = this;
        var search_con = e.detail.value;
        that.setData({
            search_con: search_con
        });
    }
});